package com.net;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import android.content.Context;
import android.gesture.Prediction;
import android.os.Handler;

import com.utils.Const;
import com.utils.PreferenceUtils;

public class TTHttpRequestControll {

	private static final ThreadPoolExecutor mThreadPoolExecutor = (ThreadPoolExecutor) Executors
			.newCachedThreadPool();
	private static Handler mHandler = null;

	private static void checkHandler() {
		try {
			if (mHandler == null) {
				mHandler = new Handler();
			}
		} catch (Exception e) {
			mHandler = null;
		}
	}

	/**
	 * 登录
	 * 
	 * @param param
	 * @param callBack
	 */
	public static void httpLogin(final Context context, final String loginName,
			final String pwd,
			final HttpRequestUtil.HttpRonspontCallBack callBack) {
		try {

			checkHandler();
			mThreadPoolExecutor.execute(new Runnable() {

				@Override
				public void run() {
					try {

						HttpRequest param = new HttpRequest();
						// param.methorid = Const.APP_URL;
						param.params.add(new RequestParam("key", "value"));

						final HttpResponse result = new HttpRequestUtil(param,
								HttpRequestUtil.HTTP_POST, callBack)
								.contentWebservice();

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								callBack.callBack(result);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void httpIndex(final Context context,
			final HttpRequestUtil.HttpRonspontCallBack callBack) {
		try {

			checkHandler();
			mThreadPoolExecutor.execute(new Runnable() {

				@Override
				public void run() {
					try {

						HttpRequest param = new HttpRequest();
						param.SERVICE_URL = PreferenceUtils.getString(context, Const.SERVICE, Const.SERVICE_URL);
						param.methorid = "GetMournHallList.ashx";
						param.params.add(new RequestParam("key", "value"));

						final HttpResponse result = new HttpRequestUtil(param,
								HttpRequestUtil.HTTP_POST, callBack)
								.contentWebservice();

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								callBack.callBack(result);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void httpMain1(final Context context,final String id,
			final HttpRequestUtil.HttpRonspontCallBack callBack) {
		try {

			checkHandler();
			mThreadPoolExecutor.execute(new Runnable() {

				@Override
				public void run() {
					try {

						HttpRequest param = new HttpRequest();
						param.SERVICE_URL = PreferenceUtils.getString(context, Const.SERVICE, Const.SERVICE_URL);
						param.methorid = "GetDefaultMournTask.ashx";
						param.params.add(new RequestParam("id", id));

						final HttpResponse result = new HttpRequestUtil(param,
								HttpRequestUtil.HTTP_POST, callBack)
								.contentWebservice();

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								callBack.callBack(result);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void httpPF(final Context context,final String taskId,final String result,
			final HttpRequestUtil.HttpRonspontCallBack callBack) {
		try {

			checkHandler();
			mThreadPoolExecutor.execute(new Runnable() {

				@Override
				public void run() {
					try {

						HttpRequest param = new HttpRequest();
						param.SERVICE_URL = PreferenceUtils.getString(context, Const.SERVICE, Const.SERVICE_URL);
						param.methorid = "MournServiceSet.ashx";
						param.params.add(new RequestParam("taskid", taskId));
						param.params.add(new RequestParam("result", result));

						final HttpResponse result = new HttpRequestUtil(param,
								HttpRequestUtil.HTTP_POST, callBack)
								.contentWebservice();

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								callBack.callBack(result);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void httpSearch(final Context context,final String id,final String date,
			final HttpRequestUtil.HttpRonspontCallBack callBack) {
		try {

			checkHandler();
			mThreadPoolExecutor.execute(new Runnable() {

				@Override
				public void run() {
					try {

						HttpRequest param = new HttpRequest();
						param.SERVICE_URL = PreferenceUtils.getString(context, Const.SERVICE, Const.SERVICE_URL);
						param.methorid = "GetMournTask.ashx";
						param.params.add(new RequestParam("id", id));
						param.params.add(new RequestParam("date", date));

						final HttpResponse result = new HttpRequestUtil(param,
								HttpRequestUtil.HTTP_POST, callBack)
								.contentWebservice();

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								callBack.callBack(result);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void httpService(final Context context,final String deadId,
			final HttpRequestUtil.HttpRonspontCallBack callBack) {
		try {

			checkHandler();
			mThreadPoolExecutor.execute(new Runnable() {

				@Override
				public void run() {
					try {

						HttpRequest param = new HttpRequest();
						param.SERVICE_URL = PreferenceUtils.getString(context, Const.SERVICE, Const.SERVICE_URL);

						param.methorid = "GetDeadBusiness.ashx";
						param.params.add(new RequestParam("deadId", deadId));

						final HttpResponse result = new HttpRequestUtil(param,
								HttpRequestUtil.HTTP_POST, callBack)
								.contentWebservice();

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								callBack.callBack(result);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void httpNote(final Context context,final String taskId,final String status,
			final HttpRequestUtil.HttpRonspontCallBack callBack) {
		try {

			checkHandler();
			mThreadPoolExecutor.execute(new Runnable() {

				@Override
				public void run() {
					try {

						HttpRequest param = new HttpRequest();
						param.SERVICE_URL = PreferenceUtils.getString(context, Const.SERVICE, Const.SERVICE_URL);
						param.methorid = "MournOperate.ashx";
						param.params.add(new RequestParam("taskId", taskId));
						param.params.add(new RequestParam("status", status));
						final HttpResponse result = new HttpRequestUtil(param,
								HttpRequestUtil.HTTP_POST, callBack)
								.contentWebservice();

						mHandler.post(new Runnable() {

							@Override
							public void run() {
								callBack.callBack(result);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
